package cn.alpha.workflow.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("testController")
public class TestController extends AbstractController 
{
	
	
	@RequestMapping("/test/resApply")
	@ResponseBody
	public Object testResApply()
	{
		System.out.println("==================>>启动流程");
		String businessKey="业务主键businessKey-tesResApply";
		Map<String,Object> proParam=new HashMap<String, Object>();
		proParam.put("resOrg", "org-3039714");
		proParam.put("applyUserId", "3020629");
        identityService.setAuthenticatedUserId("3020629");//LQ_CZJ
        ProcessInstance pro= runtimeService.startProcessInstanceByKey("resApply", businessKey,proParam);

        Long taskCount;
        Map<String,Object> task;
        Map<String,Object> taskParam;
        String taskId;
        
        System.out.println("=====================>>环节：资源申请一审");
        taskCount=taskService.createTaskQuery().processInstanceId(pro.getId()).active().count();
        System.out.println("=====================>>流程相关的任务个数："+taskCount);
        task=getNextTask("1");
        if(task==null)
        	return "资源申请一审节点出错";
        taskId=(String)task.get("id");
        taskService.claim(taskId, "1");
		taskParam=new HashMap<String, Object>();
		taskParam.put("isOk", false);
		taskService.addComment(taskId, pro.getId(), "admin驳回的comment");
		taskService.complete(taskId,taskParam);
		System.out.println("=====================>>admin驳回");
		
		System.out.println("=====================>>环节：重新填写资源申请表单");
        taskCount=taskService.createTaskQuery().processInstanceId(pro.getId()).active().count();
        System.out.println("=====================>>流程相关的任务个数："+taskCount);
        task=getNextTask("3020629");
        if(task==null)
        	return "重新填写资源申请表单 节点出错";
        taskId=(String)task.get("id");
        taskService.claim(taskId, "3020629");
        taskService.addComment(taskId, pro.getId(), "重新填写资源申请表单3020629-comment");
		taskService.complete(taskId);
		
		System.out.println("=====================>>环节：资源申请一审");
        taskCount=taskService.createTaskQuery().processInstanceId(pro.getId()).active().count();
        System.out.println("=====================>>流程相关的任务个数："+taskCount);
        task=getNextTask("1");
        if(task==null)
        	return "资源审核节点出错";
        taskId=(String)task.get("id");
        taskService.claim(taskId, "1");
		taskParam=new HashMap<String, Object>();
		taskParam.put("isOk", true);
		taskService.addComment(taskId, pro.getId(), "资源申请一审1 通过");
		taskService.complete(taskId,taskParam);//addComment必须在complement之前
		System.out.println("=====================>>1 通过");
        
		System.out.println("=====================>>环节：资源申请二审");
        taskCount=taskService.createTaskQuery().processInstanceId(pro.getId()).active().count();
        System.out.println("=====================>>流程相关的任务个数："+taskCount);
        task=getNextTask("3020708");
        if(task==null)
        	return "资源申请二审节点出错";
        taskId=(String)task.get("id");
        taskService.claim(taskId, "3020708");
		taskParam=new HashMap<String, Object>();
		taskParam.put("isOk", false);
		taskService.complete(taskId,taskParam);
		System.out.println("=====================>>3020708二审驳回");
		
		System.out.println("=====================>>环节：重新填写资源申请表单");
        taskCount=taskService.createTaskQuery().processInstanceId(pro.getId()).active().count();
        System.out.println("=====================>>流程相关的任务个数："+taskCount);
        task=getNextTask("3020629");
        if(task==null)
        	return "重新填写资源申请表单 节点出错";
        taskId=(String)task.get("id");
        taskService.claim(taskId, "3020629");
		taskService.complete(taskId);
		
		System.out.println("=====================>>环节：资源申请一审");
        taskCount=taskService.createTaskQuery().processInstanceId(pro.getId()).active().count();
        System.out.println("=====================>>流程相关的任务个数："+taskCount);
        task=getNextTask("3020661");
        if(task==null)
        	return "资源审核节点出错";
        taskId=(String)task.get("id");
        taskService.claim(taskId, "3020661");
		taskParam=new HashMap<String, Object>();
		taskParam.put("isOk", true);
		taskService.complete(taskId,taskParam);
		System.out.println("=====================>>3020661 通过");
		
		System.out.println("=====================>>环节：资源申请二审");
        taskCount=taskService.createTaskQuery().processInstanceId(pro.getId()).active().count();
        System.out.println("=====================>>流程相关的任务个数："+taskCount);
        task=getNextTask("3020708");
        if(task==null)
        	return "资源申请二审节点出错";
        taskId=(String)task.get("id");
        taskService.claim(taskId, "3020708");
		taskParam=new HashMap<String, Object>();
		taskParam.put("isOk", true);
		taskService.complete(taskId,taskParam);
		System.out.println("=====================>>3020708二审驳回");
		
		System.out.println("=====================>>环节：结束");
        taskCount=taskService.createTaskQuery().processInstanceId(pro.getId()).active().count();
        System.out.println("=====================>>流程相关的任务个数："+taskCount);
        Long proCount=runtimeService.createProcessInstanceQuery().processInstanceId(pro.getId()).count();
        System.out.println("流程结束？"+proCount);
		
		return "success";
	}
	

	@RequestMapping("/test/resPublish")
	@ResponseBody
	public Object testResPublish()
	{
		System.out.println("==================>>启动流程");
		String businessKey="业务主键businessKey-testStartVariable";
        //设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
        identityService.setAuthenticatedUserId("3020629");//LQ_CZJ
        ProcessInstance pro= runtimeService.startProcessInstanceByKey("resPublish", businessKey);

        Long taskCount;
        Map<String,Object> task;
        Map<String,Object> taskParam;
        String taskId;
        
        System.out.println("=====================>>环节：资源审核");
        taskCount=taskService.createTaskQuery().processInstanceId(pro.getId()).active().count();
        System.out.println("=====================>>流程相关的任务个数："+taskCount);
        task=getNextTask("1");
        if(task==null)
        	return "资源审核节点出错";
        taskId=(String)task.get("id");
        taskService.claim(taskId, "1");
		taskParam=new HashMap<String, Object>();
		taskParam.put("isOk", false);
		taskService.complete(taskId,taskParam);
		System.out.println("=====================>>admin驳回");
		
		System.out.println("=====================>>环节：重新编辑资源");
        taskCount=taskService.createTaskQuery().processInstanceId(pro.getId()).active().count();
        System.out.println("=====================>>流程相关的任务个数："+taskCount);
        task=getNextTask("3020629");
        if(task==null)
        	return "重新编辑资源 节点出错";
        taskId=(String)task.get("id");
        taskService.claim(taskId, "3020629");
		taskService.complete(taskId);
		
		System.out.println("=====================>>环节：资源审核");
        taskCount=taskService.createTaskQuery().processInstanceId(pro.getId()).active().count();
        System.out.println("=====================>>流程相关的任务个数："+taskCount);
        task=getNextTask("1");
        if(task==null)
        	return "资源审核节点出错";
       /* taskId=(String)task.get("id");
        taskService.claim(taskId, "1");
		taskParam=new HashMap<String, Object>();
		taskParam.put("isOk", true);
		taskService.complete(taskId,taskParam);
		System.out.println("=====================>>admin 通过");
        
		System.out.println("=====================>>环节：结束");
        taskCount=taskService.createTaskQuery().processInstanceId(pro.getId()).active().count();
        System.out.println("=====================>>流程相关的任务个数："+taskCount);
        Long proCount=runtimeService.createProcessInstanceQuery().processInstanceId(pro.getId()).count();
        System.out.println("流程结束？"+proCount);*/
        
		return "success";
	}
	
	public Map<String, Object> getNextTask(String user)
	{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		List<Task> todoList = taskService.createTaskQuery().taskCandidateOrAssigned(user).active().orderByTaskId().desc().list();
		for(Task task:todoList)
		{
			Map<String, Object> singleTask = new HashMap<String, Object>();
	        singleTask.put("id", task.getId());
	        singleTask.put("name", task.getName());
	        singleTask.put("createTime", sdf.format(task.getCreateTime()));
	        singleTask.put("pid", task.getProcessInstanceId());
	        return singleTask;
		}
		return null;
	}
}
